package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.output.MetaSystemUnitValueOutputDto;
import com.b511.swpu.cementbackend.entity.*;
import com.b511.swpu.cementbackend.exception.CementException;
import com.b511.swpu.cementbackend.repository.MetaSystemUnitValueRepository;
import com.b511.swpu.cementbackend.service.MetaSystemUnitService;
import com.b511.swpu.cementbackend.repository.MetaSystemUnitRepository;
import com.b511.swpu.cementbackend.repository.MetaUnitCategoryRepository;
import com.b511.swpu.cementbackend.repository.MetaUnitCategoryValueRepository;
import com.b511.swpu.cementbackend.util.R;
import com.b511.swpu.cementbackend.util.UserUtil;
import com.b511.swpu.cementbackend.util.enumeration.Code;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class MetaSystemUnitServiceImpl implements MetaSystemUnitService {

    private final MetaUnitCategoryRepository metaUnitCategoryRepository;

    private final MetaSystemUnitRepository metaSystemUnitRepository;

    private final MetaSystemUnitValueRepository metaSystemUnitValueRepository;

    private final UserUtil userUtil;

    public MetaSystemUnitServiceImpl(MetaUnitCategoryRepository metaUnitCategoryRepository, MetaSystemUnitRepository metaSystemUnitRepository, MetaSystemUnitValueRepository metaSystemUnitValueRepository, UserUtil userUtil) {
        this.metaUnitCategoryRepository = metaUnitCategoryRepository;
        this.metaSystemUnitRepository = metaSystemUnitRepository;
        this.metaSystemUnitValueRepository = metaSystemUnitValueRepository;
        this.userUtil = userUtil;
    }

    @Override
    @Transactional
    public R addSystemUnit(MetaSystemUnit metaSystemUnit) {
        metaSystemUnitRepository.save(metaSystemUnit);
        List<MetaUnitCategory> metaUnitCategoryList = metaUnitCategoryRepository.findAll();
        List<MetaSystemUnitValue> metaSystemUnitValueList = new ArrayList<>();
        for (MetaUnitCategory metaUnitCategory : metaUnitCategoryList) {
            metaSystemUnitValueList.add(
                    new MetaSystemUnitValue()
                            .setMetaSystemUnit(metaSystemUnit)
                            .setMetaUnitCategory(metaUnitCategory)
                            .setMetaUnitCategoryValue(metaUnitCategory.getMetaUnitCategoryValues().get(0)));
        }
        metaSystemUnitValueRepository.saveAll(metaSystemUnitValueList);
        return R.ok();
    }

    @Override
    public R getSystemUnit() {
        return R.ok().putData(getMetaSystemUnitValueOutputDto(userUtil.getCurrentUser()));
    }

    @Override
    public List<MetaSystemUnitValueOutputDto> getMetaSystemUnitValueOutputDto(SysUser sysUser) {
        if (null == sysUser) {
            throw new CementException(Code.REFRESH);
        }
        Long id = sysUser.getMetaSystemUnit().getId();
        List<MetaSystemUnitValueOutputDto> result = new ArrayList<>();
        metaSystemUnitValueRepository.findByMetaSystemUnit_Id(id).forEach(metaSystemUnitValue -> {
            result.add(new MetaSystemUnitValueOutputDto(metaSystemUnitValue.getMetaUnitCategory().getUnitName(), metaSystemUnitValue.getMetaUnitCategoryValue().getUnitName()));
        });
        return result;
    }
}
